<?php

namespace Casher;

use C\L\AdmController;
use C\M\User;

class WithdrawController extends AdmController
{



    public function indexAction()
    {
        $currentPage = $this->request->getQuery('page', 'int', 1);
        $perPage     = $this->request->getQuery('per_page', 'int', 20);
        $userMobile  = $this->request->getQuery('user', 'string', null);
        $state       = $this->request->getQuery('state', 'string', null);
        $startDate   = $this->request->getQuery('start_date', 'int', null);
        $endDate     = $this->request->getQuery('end_date', 'int', null);

        $builder = $this
        ->modelsManager
        ->createBuilder()
        ->columns([
            'withdraw_logs.*',
            'user_bank.name AS bank_name',
            'user_bank.card AS bank_card',
            'user.name AS user_name',
            'user.mobile AS user_mobile',
            'user.note as user_note '    //添加的备注
        ])
        ->from(['withdraw_logs' => 'C\M\WithdrawLog'])
        ->join(
            'C\M\UserBank',
            'user_bank.id = withdraw_logs.user_bank_id',
            'user_bank'
        )
        ->join(
            'C\M\User',
            'user.uid = withdraw_logs.user_id',
            'user'
        )
        ->orderBy('withdraw_logs.id DESC');

        if ($userMobile) {
            $user = User::findFirst([
            'conditions' => "mobile = :mobile:",
            'bind' => ['mobile' => $userMobile],
            ]);

            $builder = $builder->andWhere(
                'withdraw_logs.user_id = :user_id:',
                ['user_id' => $user->uid ?? null]
            );
        }
        if ($state) {
            $builder = $builder->andWhere('withdraw_logs.state = :state:', ['state' => $state]);
        }
        if ($startDate && $endDate) {
            $builder = $builder->andWhere(
                'withdraw_logs.created_at >= :start_date:',
                ['start_date' => date('Y-m-d H:i:s', $startDate)]
            );
            $builder = $builder->andWhere(
                'withdraw_logs.created_at <= :end_date:',
                ['end_date' => date('Y-m-d H:i:s', $endDate)]
            );
        }

        $pagination = $this->paginationByBuilder($builder, $currentPage, $perPage);

        $items = array_map(function ($item) {
            $data = $item->withdraw_logs->toArray();
            $data['bank'] = [
            'name' => $item->bank_name,
            'card' => $item->bank_card,
            ];
            $data['user'] = [
                'name'   => $item->user_name,
                'mobile' => $item->user_mobile,
                'note'   => $item->user_note,
            ];
            return $data;
        }, $pagination['items']->toArray());

        $result = array_merge($pagination, compact('items'));

        return $this->success($result);
    }




    public function successAction()
    {
        $id = $this->request->getPost('id', 'int', 0);

        try {
            return $this->s_casher_withdraw->updateState($id, 'success')
            ? $this->success()
            : $this->error();
        } catch (\Exception $error) {
            return $this->error($error->getMessage());
        }
    }

    public function failureAction()
    {
        $id = $this->request->getPost('id', 'int', 0);

        try {
            return $this->s_casher_withdraw->updateState($id, 'failure')
            ? $this->success()
            : $this->error();
        } catch (\Exception $error) {
            return $this->error($error->getMessage());
        }
    }
}
